package cn.li.chatgpt.api.service;

import cn.li.chatgpt.api.AnswerApi;
import cn.li.chatgpt.api.zsmodel.req.AnswerParam;
import cn.li.chatgpt.api.zsmodel.req.ReqData;
import cn.li.chatgpt.api.zsmodel.resp.AnswerResponse;
import cn.li.chatgpt.api.zsmodel.resp.UnAnswerResponse;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@Slf4j
public class AnswerService implements AnswerApi {

    @Override
    public UnAnswerResponse queryQuestion(String cookie, String groupId) throws IOException {
        CloseableHttpClient client = HttpClientBuilder.create().build();

        HttpGet get = new HttpGet("https://api.zsxq.com/v2/groups/"+groupId+"/topics?scope=unanswered_questions&count=20");

        get.setHeader("cookie",cookie);
        get.setHeader("Content-Type","application/json; charset=UTF-8");

        CloseableHttpResponse response = client.execute(get);
        if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
            String jsonString = EntityUtils.toString(response.getEntity());
            log.info(jsonString);
            return JSON.parseObject(jsonString,UnAnswerResponse.class);
        }else{
            throw new RuntimeException("queryQuestion ERR:"+response.getStatusLine().getStatusCode());
        }
    }

    @Override
    public boolean answer(String cookie, long topicId,boolean silenced,String text) throws IOException {
        CloseableHttpClient client = HttpClientBuilder.create().build();

        HttpPost post = new HttpPost("https://api.zsxq.com/v2/topics/"+topicId+"/answer");

        post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0");
        post.setHeader("cookie",cookie);
        post.setHeader("Content-Type","application/json; charset=UTF-8");

//        String param = "{\n" +
//                "\"req_data\": {\n" +
//                "\"text\": \"我是你爸爸\", \n" +
//                "\"image_ids\": [], \n" +
//                "\"silenced\": true}\n" +
//                "}";
        AnswerParam param =new AnswerParam(new ReqData(text,silenced));
        String jsonparam = JSON.toJSONString(param);
        StringEntity entry = new StringEntity(jsonparam, ContentType.create("text/json","UTF-8"));
        post.setEntity(entry);

        CloseableHttpResponse response = client.execute(post);

        if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
            String resp = EntityUtils.toString(response.getEntity());
            log.info(resp);
            AnswerResponse answerResponse = JSON.parseObject(resp,AnswerResponse.class);
            return answerResponse.getSucceeded();
        }else {
            throw new RuntimeException("answer Err:" + response.getStatusLine().getStatusCode());
        }
    }
}
